package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.PollutionControlDao;
import com.jiantong.work.impl.manager.PollutionControlManager;
import com.jiantong.work.model.PollutionControl;
import com.jiantong.work.service.PollutionControlWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by niufei on 2017/3/13.
 *
 */
@Slf4j
@Service
@RpcProvider
public class PollutionControlWriteServiceImpl implements PollutionControlWriteService{

    private final PollutionControlManager pollutionControlManager;

    @Autowired
    public PollutionControlWriteServiceImpl(PollutionControlManager pollutionControlManager) {
        this.pollutionControlManager = pollutionControlManager;
    }


    /**
     * 创建治污中心新单据
     * @param pollutionControl 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Map<String,Object>> create(PollutionControl pollutionControl,Integer toStatus) {
        try {
            return Response.ok(pollutionControlManager.createPollutionControl(pollutionControl,toStatus));
        }catch (Exception e){
            log.error("failed to create pollutionControl error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.failed");
        }
    }

    /**
     * 更新治污中心的单据
     * @param pollutionControl 单据对象
     * @return true or false
     */
    @Override
    public Response<Map<String,Object>> update(PollutionControl pollutionControl,Integer toStatus) {
        try{
            return Response.ok(pollutionControlManager.updatePollutionControl(pollutionControl,toStatus));
        }catch (Exception e){
            log.error("failed to update pollutionControl error :{}", Throwables.getStackTraceAsString(e));
            return Response.fail("update.failed");
        }
    }

    @Override
    public Boolean update(PollutionControl pollutionControl) {
        return pollutionControlManager.update(pollutionControl);
    }


}
